package org.boulderfoodrescue.webapp.client.donors;

import java.util.List;

import org.boulderfoodrescue.webapp.client.Presenter;
import org.boulderfoodrescue.webapp.client.events.DonorInfoUpdatedEvent;
import org.boulderfoodrescue.webapp.client.events.DonorInfoUpdatedHandler;
import org.boulderfoodrescue.webapp.client.managers.LocationInfoManager;
import org.boulderfoodrescue.webapp.client.pickuptimes.TimeSlotView;
import org.boulderfoodrescue.webapp.shared.Location;
import org.boulderfoodrescue.webapp.shared.Location.LocationType;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

public class DonorPresenter implements Presenter, DonorRecipientInterface{
	private EventBus eventBus;
	private FlowPanel container;
	private MainDonorView mainDonorView;
	private SetLocationInfoView setDonorInfoView;
	private TimeSlotView timeSlotView;

	public DonorPresenter(EventBus eventBus, String token)
	{  
		this.mainDonorView = new MainDonorView(this);
		this.setDonorInfoView = new SetLocationInfoView(LocationType.Donor);

		container = new FlowPanel();
		container.setHeight("100%");

		this.eventBus = eventBus;
		bind();

		List<Location> donors = LocationInfoManager.getDonorList();
		for(Location d : donors){
			mainDonorView.getDonorTable().addLocation(d);
		}
		
		if(token.equals("donorsNewDonor")){
			onCreateNewLocationClick();
		} else{
			container.add(mainDonorView);
		}
			
	}

	private void bind()
	{
		eventBus.addHandler(DonorInfoUpdatedEvent.getType(),
				new DonorInfoUpdatedHandler(){
			@Override
			public void onDonorAdded(DonorInfoUpdatedEvent event) {
				mainDonorView.getDonorTable().addLocation(event.getValue());
			}
			@Override
			public void onDonorRemoved(DonorInfoUpdatedEvent event) {
				mainDonorView.getDonorTable().removeLocation(event.getValue());
			}
			@Override
			public void onDonorUpdated(DonorInfoUpdatedEvent event) {
				mainDonorView.getDonorTable().updateLocation();
			}
		});
		setDonorInfoView.addValueChangeHandler(new ValueChangeHandler<Location>(){
			@Override
			public void onValueChange(ValueChangeEvent<Location> event) {
				container.clear();
				container.add(mainDonorView);
				if(event.getValue() != null)
					LocationInfoManager.saveLocationInfo(event.getValue());
				History.newItem("donors", false);
			}
		});
	}

	@Override
	public Widget getWidget() {
		return container;
	}

	@Override
	public void onCreateNewLocationClick() {
		container.clear();
		container.add(setDonorInfoView);
		setDonorInfoView.reset();
	}

	@Override
	public void onCancelNewLocation() {
		container.clear();
		container.add(mainDonorView);
		History.newItem("donors", false);
	}

	@Override
	public void onTimeSlotViewOpen(Location location) {
		container.clear();
		this.timeSlotView = new TimeSlotView(this, eventBus, location);
		container.add(timeSlotView);
	}

	@Override
	public void onTimeStotViewClose() {
		container.clear();
		container.add(mainDonorView);
	}

	@Override
	public void onEditLocation(Location location) {
		container.clear();
		container.add(setDonorInfoView);
		setDonorInfoView.setValue(location, false);
	}
}
